﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fis.Web.Rules
{

    public enum BookState
    {
        /// <summary>
        /// 无
        /// </summary>
        None,
        /// <summary>
        /// 初始化
        /// </summary>
        Init,
        /// <summary>
        /// 反初始化
        /// </summary>
        UnInit,
        /// <summary>
        /// 结帐
        /// </summary>
        Close,
        /// <summary>
        /// 反结帐
        /// </summary>
        UnClose
    }

    public class WebContext
    {
        /// <summary>
        /// 账套状态注册表
        /// </summary>
        static System.Collections.Hashtable bookStateRegistry
            = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());

        /// <summary>
        /// 注册状态
        /// </summary>
        /// <param name="bookid"></param>
        /// <param name="state"></param>
        public void RegisteState(string bookid, BookState state)
        {
            lock (bookStateRegistry.SyncRoot)
            {
                bookStateRegistry[bookid] = state;
            }
        }

        /// <summary>
        /// 释放状态
        /// </summary>
        /// <param name="bookid"></param>
        public void ReleaseState(string bookid)
        {
            lock (bookStateRegistry.SyncRoot)
            {
                if (bookStateRegistry.ContainsKey(bookid))
                    bookStateRegistry.Remove(bookid);
            }
        }

        /// <summary>
        /// 验证状态
        /// </summary>
        /// <param name="bookid"></param>
        /// <returns></returns>
        public BookState ValidState(string bookid)
        {
            if (bookStateRegistry.ContainsKey(bookid))
            {
                return (BookState)bookStateRegistry[bookid];
            }
            return BookState.None;
        }

        public string StateToString(BookState state)
        {
            string msg = string.Empty;
            switch (state)
            {
                case BookState.Init:
                    msg = "正在结束初始化，请稍候访问";
                    break;
                case BookState.UnInit:
                    msg = "正在反结束初始化，请稍候访问";
                    break;
                case BookState.Close:
                    msg = "正在结账，请稍候访问";
                    break;
                case BookState.UnClose:
                    msg = "正在反结账，请稍候访问";
                    break;

            }
            return msg;
        }

        public static bool IsBookLocked(string book, ref string msg)
        {
            WebContext web = new WebContext();
            var state = web.ValidState(book);
            msg = web.StateToString(state);
            return (state != BookState.None);
        }

        /// <summary>
        /// 锁定账套
        /// </summary>
        /// <param name="action"></param>
        /// <param name="bookid"></param>
        /// <param name="state"></param>
        public static void LockBook(string bookid, BookState state, Action action)
        {
            WebContext web = new WebContext();
            web.RegisteState(bookid, state);
            try
            {
                action.Invoke();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                web.ReleaseState(bookid);
            }

        }
    }
}
